﻿#第八章 错误、调试和测试

#1.)错误
## a.)try捕抓错误
try:
	print("try..")
	#r = 10 / 0
	#n = 10 / int('a')
	m = 10/5
	h = 10/2
except ValueError as e:
	print("except:",e)
except ZeroDivisionError as e: #可以有多个except语句
	print("except:",e)
else:
	print("这里是else输出,python在没有捕抓到异常可以执行else语句..神奇")
finally:
	print("finally:")	
print("end")

## b.)记录错误,logging模块
import logging
def foo(s):
    return 10 / int(s)
def bar(s):
    return foo(s) * 2
def main():
    try:
        bar('0')
    except Exception as e:
        logging.exception(e) #记录错误,程序依然可以继续往下执行

main()

logging.debug('debug message')  
logging.info('info message')  
logging.warning('warning message')  
logging.error('error message')  
logging.critical('critical message') 
'''
只显示了大于等于WARNING级别的日志
默认的日志级别设置为WARNING
日志级别等级CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET

//logging模块的详解
http://blog.csdn.net/zyz511919766/article/details/25136485

'''
print('logging模块 END \n')

## c.)抛出错误,raise语句

def foo1(s):
    n = int(s)
    if n==0:
        raise ValueError('invalid value: %s' % s)
    return 10 / n

def bar1():
    try:
        foo('0')
    except ValueError as e:
        print('ValueError!')
        raise #不做出处理,把错误往上抛出

bar1()

